package com.yaodian100.ec.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.ec.activity.entity.ActivityPageBlock;
import com.yaodian100.ec.activity.entity.ActivityPageBlockProduct;
import com.yaodian100.ec.activity.entity.ActivityPageBlockTemplate;
import com.yaodian100.ec.activity.service.ActivityPageBlockService;
import com.yaodian100.ec.activity.service.ActivityPageBlockTemplateService;
import com.yaodian100.ec.common.web.AbstractAction;

public class ActivityPageBlockProductAction extends AbstractAction {
	
	private static final long serialVersionUID = -3282236172415522278L;

	@Autowired
	private ActivityPageBlockService activityPageBlockService;
	@Autowired
	private ActivityPageBlockTemplateService activityPageBlockTemplateService;
	
	private Long pageId;
	private ActivityPageBlock block;
	private ActivityPageBlockTemplate template;
	private List<ActivityPageBlockProduct> blockProducts;
	
	public String editBlockProduct() {
		this.block = this.activityPageBlockService.get(this.block.getId());
		//this.template = this.activityPageBlockTemplateService.get(this.block.getBlockTemplateId());
		this.template = this.block.getTemplate();
		this.blockProducts = this.block.getProducts();
		this.validateProducts();
		return "editBlockProduct";
	}
	
	private void validateProducts() {
		int b = this.template.getBigImageCount().intValue();
		int s = this.template.getSmallImageCount().intValue();
		int total = b+s;
		for (int i=this.blockProducts.size();i<total;i++) {
			ActivityPageBlockProduct product = new ActivityPageBlockProduct();
			this.blockProducts.add(product);
		}
		
		try {
			List<String> validIds = this.activityPageBlockService.getValidShelfProducts(blockProducts);
			for (ActivityPageBlockProduct product:this.blockProducts) {
				if (product.getProductId() != null) {
					if (validIds.contains(product.getProductId())) {
						product.setValid("Y");
					} else {
						product.setValid("N");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String saveBlockProducts() {
		ActivityPageBlock pageBlock = this.activityPageBlockService.get(this.block.getId());
		pageBlock.setTitle(this.block.getTitle());
		pageBlock.setProducts(this.blockProducts);
		this.block = this.activityPageBlockService.update(pageBlock);
		this.pageId = this.block.getPageId();
		return "editPage";
	}

	public ActivityPageBlock getBlock() {
		return block;
	}

	public void setBlock(ActivityPageBlock block) {
		this.block = block;
	}

	public List<ActivityPageBlockProduct> getBlockProducts() {
		return blockProducts;
	}

	public void setBlockProducts(List<ActivityPageBlockProduct> blockProducts) {
		this.blockProducts = blockProducts;
	}

	public ActivityPageBlockService getActivityPageBlockService() {
		return activityPageBlockService;
	}

	public void setActivityPageBlockService(
			ActivityPageBlockService activityPageBlockService) {
		this.activityPageBlockService = activityPageBlockService;
	}

	public ActivityPageBlockTemplateService getActivityPageBlockTemplateService() {
		return activityPageBlockTemplateService;
	}

	public void setActivityPageBlockTemplateService(
			ActivityPageBlockTemplateService activityPageBlockTemplateService) {
		this.activityPageBlockTemplateService = activityPageBlockTemplateService;
	}

	public ActivityPageBlockTemplate getTemplate() {
		return template;
	}

	public void setTemplate(ActivityPageBlockTemplate template) {
		this.template = template;
	}

	public Long getPageId() {
		return pageId;
	}

	public void setPageId(Long pageId) {
		this.pageId = pageId;
	}

	
}
